package com.sttk.secreconeclick.bookkeep.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sttk.secreconeclick.bookkeep.BaseAc;
import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.adapter.AccountBookTypeAdapter;
import com.sttk.secreconeclick.bookkeep.bean.AccountBookBean;
import com.sttk.secreconeclick.bookkeep.bean.AccountBookTypeBean;
import com.sttk.secreconeclick.bookkeep.bean.BillTypeBean;
import com.sttk.secreconeclick.bookkeep.databinding.ActivityAddAccountBookBinding;
import com.sttk.secreconeclick.bookkeep.utils.AppSpUtils;

import java.util.ArrayList;
import java.util.List;

public class AddAccountBookActivity extends BaseAc<ActivityAddAccountBookBinding> implements View.OnClickListener, OnItemClickListener {

    public static int selPosition;
    private AccountBookTypeAdapter typeAdapter;
    private AccountBookBean accountBookBean;
    private int typePos;

    @Override
    protected int onCreate() {
        return R.layout.activity_add_account_book;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAddAccountBookBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);

        mDataBinding.rvTypeList.setLayoutManager(new LinearLayoutManager(mContext));
        typeAdapter = new AccountBookTypeAdapter();
        mDataBinding.rvTypeList.setAdapter(typeAdapter);
        typeAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvSave) {//保存
            String name = mDataBinding.etName.getText().toString();
            if (TextUtils.isEmpty(name)) {
                ToastUtils.showShort(getString(R.string.please_input_name));
                return;
            }
            accountBookBean.setName(name);
            List<AccountBookBean> accountBookList = AppSpUtils.getAccountBookList();
            if (accountBookList == null) {
                accountBookList = new ArrayList<>();
            }
            if (selPosition == -1) {
                accountBookList.add(accountBookBean);
            } else {
                accountBookList.set(selPosition, accountBookBean);
            }
            AppSpUtils.saveAccountBookList(accountBookList);
            ToastUtils.showShort(getString(R.string.save_suc));
            finish();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        typeAdapter.getItem(typePos).setSelected(false);
        typePos = position;
        typeAdapter.getItem(position).setSelected(true);
        accountBookBean.setType(position);
        accountBookBean.setTypeName(typeAdapter.getItem(position).getName());
        typeAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initData() {
        if (selPosition == -1) {
            List<BillTypeBean> listExpenditure = new ArrayList<>();
            listExpenditure.add(new BillTypeBean(R.drawable.gongzi2, R.drawable.gongzi4, R.drawable.gongzi3, "工资"));
            List<BillTypeBean> listIncome = new ArrayList<>();
            listIncome.add(new BillTypeBean(R.drawable.gongzi2, R.drawable.gongzi4, R.drawable.gongzi3, "工资"));
            accountBookBean = new AccountBookBean("", 0, getString(R.string.account_book_type1), listExpenditure, listIncome, new ArrayList<>());
        } else {
            accountBookBean = AppSpUtils.getAccountBookList().get(selPosition);
        }
        List<AccountBookTypeBean> listType = new ArrayList<>();
        listType.add(new AccountBookTypeBean(R.drawable.richang, getString(R.string.account_book_type1)));
        listType.add(new AccountBookTypeBean(R.drawable.shengyi, getString(R.string.account_book_type2)));
        listType.add(new AccountBookTypeBean(R.drawable.lvxing, getString(R.string.account_book_type3)));
        listType.add(new AccountBookTypeBean(R.drawable.baobao, getString(R.string.account_book_type4)));
        listType.add(new AccountBookTypeBean(R.drawable.xiaoyuan, getString(R.string.account_book_type5)));
        listType.add(new AccountBookTypeBean(R.drawable.jiehun, getString(R.string.account_book_type6)));

        typePos = accountBookBean.getType();
        listType.get(accountBookBean.getType()).setSelected(true);
        typeAdapter.setList(listType);

        mDataBinding.etName.setText(accountBookBean.getName());
    }
}